import { message } from "antd";
import apis from "./pushAppApi";

const initState = {
  pushAppList: [],
  totalCount: 0,
  totalPage: 0,
  showModal: false,
  currentData: {},
  appTypeList: [],
  dataSourceList: [],
  filedNameList: [],
  dataAccessMethodList: [],
  tableList: [],
  pushAppDetail: {},
  filter: {
    appName: undefined,
    creator: undefined,
    appType: undefined,
    pageIndex: 1,
    pageSize: 10
  }
};

export default {
  namespace: "pushApp",
  state: { ...initState },
  effects: {
    *pushAppConfigQuery({ payload = {} }, { call, put, select }) {
      const filter = yield select(state => state.pushApp.filter);
      const newFilter = { ...filter, ...payload };
      yield put({ type: "update/filter", payload: newFilter });
      const { data: pushAppList, totalCount, totalPage } = yield call(
        apis.pushAppConfigQuery,
        newFilter
      );
      yield put({
        type: "save/pushAppList",
        payload: { pushAppList, totalCount, totalPage }
      });
    },
    *pushAppConfigSave({ payload = {} }, { call, put }) {
      const { errCode, errMsg } = yield call(apis.pushAppConfigSave, payload);
      if (errCode === 0) {
        yield put({ type: "pushAppConfigQuery", payload: { pageIndex: 1 } });
        message.success(errMsg);
        yield put({
          type: "setState",
          payload: { showModal: false, currentData: {} }
        });
      }
    },
    // 推送应用删除
    *delPushApp({ payload }, { call, put }) {
      const { errCode, errMsg } = yield call(apis.delPushApp, payload);
      if (errCode === 0) {
        yield put({ type: "pushAppConfigQuery", payload: { pageIndex: 1 } });
        message.success(errMsg);
      }
    },
    *getAppType(empty, { call, put }) {
      const { errCode, data = [] } = yield call(apis.getAppType);
      if (errCode === 0) {
        yield put({ type: "save/appTypeList", payload: data });
      }
    },
    *getDataSource(empty, { call, put }) {
      const { errCode, data = [] } = yield call(apis.getDataSource);
      if (errCode === 0) {
        yield put({ type: "setState", payload: { dataSourceList: data } });
      }
    },
    *pushAppConfigById({ payload }, { call, put }) {
      const { errCode, data } = yield call(apis.pushAppConfigById, payload);
      if (errCode === 0) {
        yield put({ type: "setState", payload: { pushAppDetail: data } });
      }
    },
    *getDataAccessMethod(empty, { call, put }) {
      const { errCode, data = [] } = yield call(apis.getDataAccessMethod);
      if (errCode === 0) {
        yield put({
          type: "setState",
          payload: { dataAccessMethodList: data }
        });
      }
    },
    *getFiledName(empty, { call, put }) {
      const { errCode, data = [] } = yield call(apis.getFiledName);
      if (errCode === 0) {
        yield put({
          type: "setState",
          payload: { filedNameList: data }
        });
      }
    },
    *getDataHub(empty, { call, put }) {
      const { errCode, data = [] } = yield call(apis.getDataHub);
      if (errCode === 0) {
        yield put({
          type: "setState",
          payload: { tableList: data }
        });
      }
    }
  },
  reducers: {
    "save/pushAppList": (
      state,
      { payload: { pushAppList = [], totalCount = 0, totalPage = 0 } }
    ) => ({
      ...state,
      pushAppList,
      totalCount,
      totalPage
    }),
    "save/appTypeList": (state, { payload: appTypeList = [] }) => ({
      ...state,
      appTypeList
    }),
    "update/filter": (state, { payload = {} }) => ({
      ...state,
      filter: payload
    }),
    setState: (state, { payload = {} }) => ({
      ...state,
      ...payload
    }),
    clearState: () => ({ ...initState })
  }
};
